package com.zatech.cgnci.project.job;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zatech.cgnci.project.base.utils.DateUtils;
import com.zatech.cgnci.project.base.utils.VerifyUtils;
import com.zatech.cgnci.project.entity.CgnciApprovalConfigRoleUserDO;
import com.zatech.cgnci.project.service.CgnciApprovalConfigRoleUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 工作流授权代理人赋予权限的定时任务
 *
 * @author user
 */
@Component
@Slf4j
public class CgnciApprovalConfigGrantAuthUserJob {

    @Resource
    CgnciApprovalConfigRoleUserService approvalConfigRoleUserService;

    @Scheduled(cron = "0 1 0 * * ?") // 每天0点1分执行一次
    public void grantAuthUserJob() {
        log.info("工作流授权代理人赋予权限的定时任务开始。。。");
        // 查询授权人不为空,当前日期等于授权开始日期列表
        List<CgnciApprovalConfigRoleUserDO> list = approvalConfigRoleUserService.list(new LambdaQueryWrapper<CgnciApprovalConfigRoleUserDO>()
                .isNotNull(CgnciApprovalConfigRoleUserDO::getAuthCode)
                // 当前日期等于授权开始日期
                .eq(CgnciApprovalConfigRoleUserDO::getStartDate, DateUtils.dateToString(new Date(),DateUtils.FORMAT_3)));
        if (VerifyUtils.isNotEmpty(list)) {
            list.forEach(approvalConfigRoleUserDO -> {
                approvalConfigRoleUserService.grantAuthUser(approvalConfigRoleUserDO);
            });
        }
        log.info("工作流授权代理人赋予权限的定时任务结束。。。");
    }
}
